package hasthi.tools;

import hasthi.common.HasthiException;
import hasthi.common.HasthiException.FaultCode;
import hasthi.test.FooDocument;
import hasthi.test.FooResponseDocument;

import java.io.IOException;
import java.net.ServerSocket;

import javax.xml.namespace.QName;

import xsul.invoker.soap_over_http.SoapHttpDynamicInfosetInvoker;
import xsul.processor.DynamicInfosetProcessorException;
import xsul.xbeans_util.XBeansUtil;
import xsul.xservices_xbeans.XmlBeansBasedService;
import xsul.xservo_soap_http.HttpBasedServices;

public class DummyUnManagedService {
    private String  serviceUrl;
    public FooResponseDocument foo(FooDocument request) throws InterruptedException{
        long start = System.currentTimeMillis();
        while(System.currentTimeMillis()-start < 10){
            System.getenv();
        }
        FooResponseDocument document = FooResponseDocument.Factory.newInstance();
        document.addNewFooResponse().setOut(request.getFoo().getIn());
        return document;
    }
    public int start() throws HasthiException{
        return start(0);
    }
    
    public int start(int port) throws HasthiException{
        try {
            if(port == 0){
                ServerSocket tempSocket = new ServerSocket(0);
                port = tempSocket.getLocalPort();
                tempSocket.close();
            }
            HttpBasedServices httpServices = new HttpBasedServices(port);
            String cwsdlLoc = Thread.currentThread().getContextClassLoader().getResource("hasthi/tools/dummyservice.wsdl").toString();
            
            
            
            XmlBeansBasedService service = new XmlBeansBasedService("dummy", cwsdlLoc,this);
            httpServices.addService(service).startService();
            
            serviceUrl = service.getWsdlLocationToUse().replaceAll("\\?wsdl", "");
            return port;
        } catch (DynamicInfosetProcessorException e) {
            throw new HasthiException(e,FaultCode.InitalizationError);
        } catch (IOException e) {
            throw new HasthiException(e,FaultCode.InitalizationError);
        }
    }
    
    
    public void invoke(){
        SoapHttpDynamicInfosetInvoker invoker = new SoapHttpDynamicInfosetInvoker(this.serviceUrl);
        FooDocument fooRequest = FooDocument.Factory.newInstance();
        fooRequest.addNewFoo().setIn("Hello");
        invoker.invokeMessage(XBeansUtil.xmlObjectToXmlElement(fooRequest));
    }
    
    
    
    public String getServiceUrl() {
        return serviceUrl;
    }

    //private static Random rand = new Random();
    public static QName randomServiceName(int port){
        return new QName("http://extreme.indiana.edu/dummyService","Foo"+port);
    }
    
    public static void main(String[] args) throws Exception{
        int port = 5678 ;
        if(args.length > 0){
            port = Integer.parseInt(args[0]);
        }

        DummyUnManagedService dummyService = new DummyUnManagedService();
        dummyService.start(port);
        System.out.println(dummyService.getServiceUrl());
        System.in.read();
    }
}
